// pages/mall/gone/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0, shequ_id: 0,
    code: '', show_code: '-', shop_id: 0,
    id: 0, title: '-', price: 0, num: 0,
    cover: '', visit_total: 0, lasttime: '-',
    price_old: 0, sale: 0, detail: '', 
    pop_tmp: 0, pop_cate: 0, pop_attr: 0, //弹窗是否显示
    cateList: [], attrList: [], attrHei: 100,
    attrId: 0, attrKey: '', attrVal: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOne(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.userInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  userInfo: function () {
    var uinfo = wx.getStorageSync('uinfo');
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none',
      }); return
    }
    this.setData({
      uid: uinfo.uid, shequ_id: uinfo.shequ_id,
    });
  },

  // 详情
  getOne: function (e) {
    var id = e.id ? e.id : 0;
    var code = e.code ? e.code : '';
    console.log(id, code); //return
    var uri = code=='' ? 'onebyid' : 'one';
    var _this = this;
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-mall/goods/' + uri,
      data: { code: code, id: id, },
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var attrHei = res.data.attrs.length * 60 + 100;
        _this.setData({
          id: res.data.id,
          code: res.data.code,
          show_code: res.data.show_code,
          gname: res.data.gname, 
          visit_total: res.data.visit_total, 
          lasttime: res.data.lasttime,
          cover: res.data.cover, 
          detail: res.data.detail, 
          price: res.data.price, 
          price_old: res.data.price_old, 
          sale: res.data.sale,
          attrs: res.data.attrs,
          attrHei: attrHei,
          pop_attr: 0,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 立即下单
  toOrder: function (e) {
    var formData = {
      id: this.data.id,
      uid: this.data.uid,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-mall/gorder/add',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        wx.navigateTo({
          url: '/pages/my/gorder/index',
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },
})